<template>
	<section v-loading="listLoading">
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<!--<el-form-item>
					<el-input v-model="filters.content" clearable placeholder="名称"></el-input>
				</el-form-item>-->
				<el-form-item class="fr">
				    <el-button type="primary" size="medium" plain @click="handleAdd">新增</el-button>
			   </el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="users" stripe highlight-current-row border @selection-change="selsChange" style="width: 100%;">
            <el-table-column prop="indexId" label="ID" min-width="40" align="center">
			</el-table-column>
			<el-table-column prop="labelName" label="名称" min-width="60" align="center">
			</el-table-column>
			
			<el-table-column label="操作" min-width="100" align="center">
				<template slot-scope="scope">
					<el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="mini" plain @click="handleDel(scope.row.childId)">删除</el-button>
					<el-button size="mini" type="success" plain @click="handleGoods(scope.row)">商品</el-button>
				</template>
			</el-table-column>
		</el-table>

		
		<!--新增界面-->
		<el-dialog title="新增" :visible.sync="addFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="addForm" label-width="80px" ref="addForm">
				<el-form-item label="名称" prop="labelName"  :rules="[{ required: true, message: '请填写名称', trigger: 'blur' }]">
                    <el-input v-model="addForm.labelName" />
                </el-form-item>
                
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="addFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--编辑界面-->
		<el-dialog title="编辑" :visible.sync="editFormVisible" :close-on-click-modal="false" width="500px">
			<el-form :model="editForm" label-width="80px" ref="editForm">
				<el-form-item label="名称" prop="labelName"  :rules="[{ required: true, message: '请填写名称', trigger: 'blur' }]">
                    <el-input v-model="editForm.labelName" />
                </el-form-item>
                
				
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="editFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>
		
		
		
		<!--商品-->
		<el-dialog title="模块商品" :visible.sync="goodsFormVisible" :close-on-click-modal="false" width="800px">
			<el-button type="primary" size="medium" plain @click="handleAddGoods" style="margin-bottom: 20px;">新增</el-button>
			<el-table :data="goodsList" stripe highlight-current-row border @selection-change="selsChange" style="width: 100%;">
	            <el-table-column prop="goodsId" label="商品ID" min-width="40" align="center">
				</el-table-column>
				<el-table-column prop="goodsName" label="商品名称" min-width="60" align="center">
				</el-table-column>
				<el-table-column prop="goodsPrice" label="商品价格" min-width="60" align="center">
				</el-table-column>
				<el-table-column prop="goodsImg" label="商品图片" align="center" min-width="100">
					<template slot-scope="scope">
	                    <el-popover placement="top-start" title="" trigger="click">
	                        <img :src="scope.row.goodsImg" alt="" style="width: 500px">
	                        <img slot="reference" :src="scope.row.goodsImg" style="width: 50px;height: 50px;border-radius: 5px;object-fit: cover;">
	                    </el-popover>
	                </template>
	            </el-table-column>
				<el-table-column label="操作" width="80" align="center">
					<template slot-scope="scope">
						<el-button type="danger" size="mini" plain @click="handleDelGoods(scope.row.goodsId)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-dialog>
		
		<!--商品-->
		<el-dialog title="模块商品" :visible.sync="listFormVisible" :close-on-click-modal="false" width="800px" style="margin-top: -80px;">
			<el-table :data="list" stripe highlight-current-row border @selection-change="selsChange" style="width: 100%;">
				<el-table-column type="selection" width="40"></el-table-column>
	            <el-table-column prop="goodsId" label="商品ID" min-width="40" align="center">
				</el-table-column>
				<el-table-column prop="goodsInfo.name" label="商品名称" min-width="60" align="center">
				</el-table-column>
				<el-table-column prop="categoryName" label="分类" min-width="60" align="center">
				</el-table-column>
				<el-table-column prop="goodsInfo.counterPrice" label="现价" min-width="60" align="center">
			    </el-table-column>
				<el-table-column prop="goodsInfo.picUrl" label="商品图片" align="center" min-width="100">
					<template slot-scope="scope">
	                    <el-popover placement="top-start" title="" trigger="click">
	                        <img :src="scope.row.goodsInfo.picUrl" alt="" style="width: 500px">
	                        <img slot="reference" :src="scope.row.goodsInfo.picUrl" style="width: 50px;height: 50px;border-radius: 5px;object-fit: cover;">
	                    </el-popover>
	                </template>
	            </el-table-column>
			</el-table>
			<el-pagination layout="prev, pager, next, jumper" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
			<div slot="footer" class="dialog-footer">
				<el-button size="mini" @click.native="listFormVisible = false">取消</el-button>
				<el-button size="mini" type="primary" @click.native="goodsSubmit" :disabled="this.sels.length===0">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import { client } from '../../common/js/alioss'
	import util from '../../common/js/util'

	export default {
		data() {
			return {
				
				filters: {
					content: '',
				},
				
                data:{},
				users: [],
				data:[],
				sels: [],
				total: 0,
				page: 1,
				pageSize: 10,
				listLoading: false,
				
				
				addFormVisible: false,
				addLoading: false,
				addForm: {
					indexId: '',
					labelName: '',
				},
				editFormVisible: false,
				editLoading: false,
				editForm: {
					indexId: '',
					labelName: '',
				},
				goodsList:[],
				goodsFormVisible:false,
				childId:'',
				list:[],
				listFormVisible:false,
				sels:[]
			}
		},
		
		methods: {
			//二级
			indexChild(index, row, href) {
				console.log(href, '1')
				this.$router.push({
					path: '/' + href,
					query: {
						id: row.indexId
					}
				})
			},
			//添加图片
			dialogClose(){
                this.fileList = [];
            },
			
			beforeAvatarUpload(file) {
//              this.listLoading = true;
			},//图片上传到阿里云
            Upload1(file, index) {
				var fileName = 'webStorage/web_' + file.file.uid+Math.floor(Math.random()*10000) + file.file.name.substr(file.file.name.indexOf("."));
				client().put(fileName, file.file).then(result => {
					this.addForm[index] = result.url;
					console.log(this.addForm[index]);
				}).catch(function (err) {
                    console.log(err);
	 			});
            },
            Upload2(file, index) {
				var fileName = 'webStorage/web_' + file.file.uid+Math.floor(Math.random()*10000) + file.file.name.substr(file.file.name.indexOf("."));
				client().put(fileName, file.file).then(result => {
					this.editForm[index] = result.url;
				}).catch(function (err) {
                    console.log(err);
	 			});
           },
			
			//获取用户列表
			getUsers:function (){
				this.listLoading = true;
				let para = {
					page: this.page,
					size: this.pageSize,
					content: this.filters.content,
					indexId:this.$route.query.id,
				};
				var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
			    });  
			    instance.post('admin/platform/manage/getHomeModuleChildList', this.qs.stringify(para)).then(data => {
					let  res = data.data;
					console.log(data);
					if (res.code !== 0) {
						this.$message({
							message: res.msg,
							type: 'error'
						});
					} else {
						this.total = res.total;
						this.users = res.data;
						this.listLoading = false;
					}
				});
			},
			//获取商品列表
			getGoods:function (){
				let para = {
					page: this.page,
					size: this.pageSize,
					isOnSale:true
				};
				var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
				});  
				instance.post('admin/goods/getGoodsList', this.qs.stringify(para)).then(data => {
					let  res = data.data;
					console.log(data);
					if (res.code !== 0) {
						this.$message({
							message: res.msg,
							type: 'error'
						});
					} else {
						this.total = res.total;
						this.list = res.rows;
					}
				});
			},
			
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
				this.addForm = {
					labelName: '',
					indexId:this.$route.query.id,
				};
			},
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
                            let para = Object.assign({}, this.addForm);
                            var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
						    });  
						    instance.post('admin/platform/manage/addHomeModuleChild', this.qs.stringify(para)).then(data => {
								let  res = data.data;
								if (res.code !== 0) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.addLoading = false;
									this.$message({
										message: '成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
				
				this.editFormVisible = true;
				this.editForm.childId = row.childId;
				this.editForm.labelName = row.labelName;
			},
			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							let para = {
								childId:this.editForm.childId,
								labelName:this.editForm.labelName,
							}
							var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
						    });  
						    instance.post('admin/platform/manage/updHomeModuleChild', this.qs.stringify(para)).then(data => {
								
								let  res = data.data;
								if (res.code !== 0) {
									this.$message({
										message: res.msg,
										type: 'error'
									});
								} else {
									this.editLoading = false;
									this.$message({
										message: '提交成功',
										type: 'success'
									});
									this.$refs['editForm'].resetFields();
									this.editFormVisible = false;
									this.getUsers();
								}
							});
						});
					}
				});
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.handleDel(ids);
			},
			//删除
			handleDel(childId) {
				this.$confirm('确认删除以及子级分类吗?', '提示', {}).then(() => {
					this.listLoading = true;
					var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
				    });  
				    instance.post('admin/platform/manage/delHomeModuleChild', this.qs.stringify({childId: childId})).then(data => {
						
						let  res = data.data;
						if (res.code !== 0) {
							this.$message({
								message: res.msg,
								type: 'error'
							});
						} else {
							this.$message({
								message: '成功',
								type: 'success'
							});
							this.getUsers();
						}
					})
				}).catch(() => {

				});
			},
			//商品弹框
			handleGoods(row){
				this.childId = row.childId
				this.goodsList = row.goodsList
				this.goodsFormVisible = true
			},
			//删除商品
			handleDelGoods(goodsId) {
				this.$confirm('确定删除该商品吗?', '提示', {}).then(() => {
					this.listLoading = true;
					var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
				    });  
				    instance.post('admin/platform/manage/delHomeModuleGoods', this.qs.stringify({childId: this.childId,goodsIds:goodsId})).then(data => {
						
						let  res = data.data;
						if (res.code !== 0) {
							this.$message({
								message: res.msg,
								type: 'error'
							});
						} else {
							this.$message({
								message: '成功',
								type: 'success'
							});
							this.goodsFormVisible = false
							this.getUsers();
						}
					})
				}).catch(() => {

				});
			},
			//打开商品列表接口
			handleAddGoods(){
				this.listFormVisible = true
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getGoods();
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//添加商品
			goodsSubmit: function () {
				this.$confirm('确认提交吗？', '提示', {}).then(() => {
                    let para = {
                    	childId:this.childId,
                    	goodsIds:this.sels.map(item => item.goodsId).toString()
                    }
                    var instance = this.$http.create({headers: {'content-type': 'application/x-www-form-urlencoded','admin_five_palace_token':JSON.parse(sessionStorage.getItem('token'))},
				    });  
				    instance.post('admin/platform/manage/addHomeModuleGoods', this.qs.stringify(para)).then(data => {
						let  res = data.data;
						if (res.code !== 0) {
							this.$message({
								message: res.msg,
								type: 'error'
							});
						} else {
							this.$message({
								message: '成功',
								type: 'success'
							});
							this.goodsFormVisible = false
							this.listFormVisible = false
							this.getUsers();
						}
					});
				});
			},
		},
		mounted() {
			this.getGoods()
			this.getUsers();
		}
	}

</script>
<style>

</style>

<style>
	.advice{
		margin-left: 80px;
		margin-bottom: 10px;
		font-size: 12px;
	}
</style>

